package com.qf.service;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class StorageServiceImpl implements StorageService{

    @Autowired
    private DtsStorageMapper storageMapper;
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;
    @Override
    public IPage<DtsStorage> queryStoragePage(String key, String name, Integer page, Integer limit, String sort, String order) {
        IPage<DtsStorage> iPage = new Page<>(page,limit);
        QueryWrapper<DtsStorage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("deleted",false);
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("key",key);
        }
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name",name);
        }
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)) {
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                queryWrapper.orderByDesc(sort);
            }
        }
        IPage<DtsStorage> selectPage = storageMapper.selectPage(iPage, queryWrapper);
        return selectPage;
    }

    @Override
    public DtsStorage insertStorage(String name, Integer size, String extension, byte[] bytes) throws Exception {
        String key = UUID.randomUUID().toString();
        String filename=key+extension;
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, filename, bytes);
        DtsStorage storage = new DtsStorage();
        storage.setDeleted(false);
        storage.setAddTime(new Date());
        storage.setUpdateTime(new Date());
        storage.setKey(filename);
        storage.setName(name);
        storage.setSize(size);
        storage.setType(extension);
        storage.setUrl(url);
        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public int updateStorage(DtsStorage storage) {
        storage.setUpdateTime(new Date());
        int count = storageMapper.updateById(storage);
        return count;
    }

    @Override
    public void deleteStorage(String key) {
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,key);
        QueryWrapper<DtsStorage> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("`key`",key);
        storageMapper.delete(queryWrapper);

    }
}
